home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / tdecfg.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  28KB  |  894 lines

  1. /*
  2.  * A configuration utility was written to customize the tde executable file.
  3.  * You only need one file to run tde - the executable.  No configuration files
  4.  * to worry about.
  5.  *
  6.  * With this version, there is no need to figure the offsets each time
  7.  * tde.exe is modified.  The original program to find the offsets was written
  8.  * by Jim Lee, jlee@ece.orst.edu.  Simple pattern matching machines are used
  9.  * to find the offsets.  On one pass thru tde.exe, the machines will quickly
  10.  * find all signatures.
  11.  *
  12.  * ------------------------------------------------------------------------
  13.  * Jim Lee, jlee@ece.orst.edu, sent working code for finding the offsets of
  14.  * of various data structures in tde.exe.  Hoping to improve on the brute
  15.  * force method, I tried using a more complicated algorithm.  Needless to
  16.  * say and standard operating procedure, I did not correctly implement
  17.  * the more complicated algorithm.  For example,
  18.  *
  19.  *     my implementation would not find  "$  help"  in the string "$$  help"
  20.  *
  21.  * All bugs in Jim Lee's working code were introduced by me, Frank.
  22.  *
  23.  * Jimmy Thompson, jimmy_t@verifone.com, found the error and provided
  24.  * working code for the Knuth-Morris-Pratt algorithm.  Jimmy, thank you
  25.  * for the error report and the solution.
  26.  *
  27.  * That's what I get for acting like I'm so smart.  Not only did I
  28.  * incorrectly implement the Aho-Corasick algorithm, but I should have
  29.  * used the Knuth-Morris-Pratt algorithm.
  30.  *
  31.  *                   Frank Davis, August, 29, 1993, TDE 3.1
  32.  * ------------------------------------------------------------------------
  33.  *
  34.  * See:
  35.  *
  36.  *   Donald Ervin Knuth, James H. Morris, Jr., and Vaughan R. Pratt,
  37.  *     "Fast Pattern Matching in Strings."  _SIAM Journal on Computing_
  38.  *     6 (No. 2): 323-350, 1977.
  39.  *
  40.  *   Robert Sedgewick, _Algorithms in C_, Addison-Wesley, Reading, Mass.,
  41.  *     1990, Chapter 19, "String Searching", pp. 277-292, ISBN 0-201-51425-7
  42.  *
  43.  *
  44.  *                        Knuth-Morris-Pratt in TDE
  45.  *
  46.  * To be safe and sure, the KMP algorithm is pretty much based on the
  47.  * description given in _SIAM J on Comp_.  Hints for KMP in C are from
  48.  * Dr. Sedgewick's book.  Actually, on average, finding a *text* string with
  49.  * KMP is not that much faster, if any, than a brute force search.  However,
  50.  * using KMP for finding several strings at once is probably faster than
  51.  * a brute force implementation.  Anyway, I think I got the implementation
  52.  * right, this time...
  53.  *
  54.  *
  55.  * Program name:  tdecfg
  56.  * Author:        Frank Davis
  57.  * Date:          October 5, 1991
  58.  * Date:          June 5, 1993
  59.  *
  60.  * This program is released into the public domain.  You may distribute
  61.  * it freely, Frank Davis.
  62.  */
  63.  
  64.  
  65. #include <bios.h>
  66. #include <dos.h>
  67. #include <io.h>
  68. #include <malloc.h>
  69. #include <stdlib.h>
  70. #include <stdio.h>
  71. #include <string.h>
  72.  
  73. #include "tdecfg.h"
  74.  
  75.  
  76. struct vcfg cfg;                /* video stuff */
  77. FILE *tde_exe;                  /* FILE pointer to tde.exe */
  78.  
  79. long sort_offset;
  80. long mode_offset;
  81. long color_offset;
  82. long macro_offset;
  83. long keys_offset;
  84. long two_key_offset;
  85. long help_offset;
  86.  
  87. struct screen cfg_choice[] = {
  88.    {5,25,"1.  Change colors" },
  89.    {7,25,"2.  Redefine keys" },
  90.    {9,25,"3.  Install new help screen" },
  91.   {11,25,"4.  Set default modes" },
  92.   {13,25,"5.  Install permanent macro file" },
  93.   {15,25,"6.  Read in a configuration file" },
  94.   {17,25,"7.  Exit" },
  95.  {20,20,"Please enter choice: " },
  96.   {0,0,NULL}
  97. };
  98.  
  99.  
  100. char *greatest_composer_ever = "W. A. Mozart, 1756-1791";
  101.  
  102.  
  103. /*
  104.  * Name:    main
  105.  * Date:    October 5, 1991
  106.  * Notes:   Strategy is fairly straight forward -  1) initialize all the
  107.  *          variables  2) show the user a color sample  3) make the changes
  108.  *          permanent if desired.
  109.  */
  110. void main( int argc, char *argv[] )
  111. {
  112. int  rc;
  113. int  c;
  114. char fname[82];
  115. char *buff;
  116.  
  117.    /*
  118.     * lets get a 8k buffer for our pattern matching machines.
  119.     */
  120.    if ((buff = malloc( 8200 )) == NULL) {
  121.       puts( "\nNot enough memory." );
  122.       exit( 1 );
  123.    }
  124.  
  125.    puts( "\nEnter tde executable file name (<Enter> = \"tde.exe\")  :" );
  126.    gets( fname );
  127.  
  128.    if (strlen(fname) == 0)
  129.       strcpy( fname, "tde.exe" );
  130.  
  131.    if ((rc = access( fname, EXIST )) != 0) {
  132.       puts( "\nFile not found." );
  133.       exit( 1 );
  134.    } else if ((tde_exe = fopen( fname, "r+b" )) == NULL ) {
  135.       puts( "\nCannot open executable file." );
  136.       exit( 2 );
  137.    }
  138.    if ((rc = find_offsets( buff )) == ERROR)
  139.       puts( "\nFatal error finding offsets.\n" );
  140.    free( buff );
  141.    if (rc == ERROR)
  142.       exit( 3 );
  143.  
  144.    video_config( );
  145.    cls( );
  146.    show_box( 0, 0, cfg_choice, NORMAL );
  147.    for (rc=0; rc != 1;) {
  148.       xygoto( 42, 20 );
  149.       c = getkey( );
  150.       while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' &&
  151.              c != '6' && c != '7')
  152.          c = getkey( );
  153.       switch (c) {
  154.          case '1' :
  155.             tdecolor( );
  156.             show_box( 0, 0, cfg_choice, NORMAL );
  157.             break;
  158.          case '2' :
  159.             tdekeys( );
  160.             show_box( 0, 0, cfg_choice, NORMAL );
  161.             break;
  162.          case '3' :
  163.             tdehelp( );
  164.             show_box( 0, 0, cfg_choice, NORMAL );
  165.             break;
  166.          case '4' :
  167.             tdemodes( );
  168.             show_box( 0, 0, cfg_choice, NORMAL );
  169.             break;
  170.          case '5' :
  171.             tdemacro( );
  172.             show_box( 0, 0, cfg_choice, NORMAL );
  173.             break;
  174.          case '6' :
  175.             tdecfgfile( );
  176.             show_box( 0, 0, cfg_choice, NORMAL );
  177.             break;
  178.          case '7' :
  179.             rc = 1;
  180.             break;
  181.       }
  182.    }
  183.    fcloseall( );
  184.    puts( " " );
  185.    puts( " " );
  186. }
  187.  
  188.  
  189. /***********************  original comments  *************************/
  190. /*
  191. ** OFFSETS.C    -       Automatically scan tde.exe for config offsets
  192. **
  193. ** Author:  Jim Lee (jlee@ece.orst.edu)
  194. **   Date:  5/12/93
  195. ** Status:  Released to the public domain
  196. **
  197. **      This little utility takes the drudgery out of updating tdecfg.h
  198. **      every time you re-compile tde.exe.  Just remove the hard-coded
  199. **      offsets in tdecfg.h and replace them with '#include "newoff.h"'.
  200. **      Then run 'offsets tde.exe > newoff.h'.  Now re-compile tdecfg
  201. **      and you're done!
  202. **
  203. */
  204. /*****************************   end   *******************************/
  205.  
  206.  
  207. /*
  208.  * Name:    build_next_table
  209.  * Date:    August 29, 1993
  210.  * Passed:  pattern:  pattern to look for
  211.  *          next:  array for failure links
  212.  *          m:     length of pattern.
  213.  * Notes:   the algorithm for building the next table is based the description
  214.  *           by KMP in _SIAM J Comp_, page 328.  Hints for a C implementation
  215.  *           are from Dr. Sedgewick's book, page 283.
  216.  *
  217.  *          don't make the mistake I did.  the first character in the
  218.  *           sort signature is '\0'.  using strlen( ) to get the pattern
  219.  *           length don't work too good for patterns with '\0' in them.
  220.  */
  221. void build_next_table( char *pattern, char *next, int m )
  222. {
  223. int  j;
  224. int  t;
  225. int  len;
  226.  
  227.    len = m;
  228.    t = next[0] = -1;
  229.    j = 0;
  230.    while (j < len) {
  231.       while (t >= 0  &&  pattern[j] != pattern[t])
  232.          t = (int)next[t];
  233.       j++;
  234.       t++;
  235.       next[j] =  pattern[j] == pattern[t]  ?  next[t]  :  (char)t;
  236.    }
  237. }
  238.  
  239.  
  240. /*
  241.  * Name:    find_offsets
  242.  * Date:    June 5, 1993
  243.  *          August 29, 1993
  244.  * Notes:   to increase the speed, we use a pattern matching machine
  245.  *           for each of the signatures.  on one pass through the file, all 7
  246.  *           signatures will be found.  it's also a little faster if we read
  247.  *           the file in big chunks.  also note that we never have to back-up
  248.  *           or reread the file.
  249.  *
  250.  *          this implementation is pretty much based on the description by
  251.  *           KMP in _SIAM J Comp_, page 326 (middle of the page.)  hints for
  252.  *           a C implementation are from Dr. Sedgewick's book, page 282, 285.
  253.  *
  254.  *   original KMP implementation by:  James H. Thompson, jimmy_t@verifone